package conan.dataSource;

import conan.DomainLayer.Kunde;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;

public class KundeMapper
{
    public Kunde getKunde(int kundeId, Connection conn)
    {
        Kunde kunde = null;
        String SQLString1 = // get Kunde
            "select * " + "from kunde " + "where KuID = ?";
        PreparedStatement statement = null;

        try
        {
            //=== get Kunde
            statement = conn.prepareStatement(SQLString1);
            statement.setInt(1, kundeId);
            ResultSet rs = statement.executeQuery();
            if (rs.next())
            {

                kunde = new Kunde(rs.getInt("kuid"), rs.getString("navn"));

            }


        }
        catch (Exception e)
        {
            System.out.println("Fail in KundeMapper - getKunde");
            System.out.println(e.getMessage());
        }
        return kunde;
    }

    public boolean insertKunde(ArrayList<Kunde> kundeListe, 
                               Connection conn) throws SQLException
    {
        int rowsInserted = 0;
        String SQLString = "insert into kunde values (?,?)";
        PreparedStatement statement = null;

        statement = conn.prepareStatement(SQLString);

        for (int i = 0; i < kundeListe.size(); i++)
        {
            Kunde kunde = kundeListe.get(i);
            statement.setInt(1, kunde.getKundeId());
            statement.setString(2, kunde.getKundeNavn());
            rowsInserted += statement.executeUpdate();
        }
        //System.out.println(rowsInserted == ol.size());
        return (rowsInserted == kundeListe.size());
    }

    public boolean updateKunde(ArrayList<Kunde> kunde, 
                               Connection conn) throws SQLException
    {
        int rowsUpdated = 0;
        String SQLString = 
            "update kunde " + "set kuid = ?, name = ?"; // and ver = ?";
        PreparedStatement statement = null;

        statement = conn.prepareStatement(SQLString);
        for (int i = 0; i < kunde.size(); i++)
        {
            Kunde o = kunde.get(i);
            statement.setInt(1, o.getKundeId());
            statement.setString(2, o.getKundeNavn());
            //statement.setInt(3, o.getVer()); // old ver
            rowsUpdated += statement.executeUpdate();
            //FIXME o.incrVer(); // incr ver in current order
        }
        //System.out.println(rowsUpdated == ol.size());
        return (rowsUpdated == kunde.size());
    }


    public int getNextKundeNo(Connection conn)
    {
        /*
        int nextOno = 0;
        String SQLString = "select orderseq.nextval  " + "from dual";
        PreparedStatement statement = null;
        try
        {
            statement = conn.prepareStatement(SQLString);
            ResultSet rs = statement.executeQuery();
            if (rs.next())
            {
                nextOno = rs.getInt(1);
            }
        }
        catch (Exception e)
        {
            System.out.println("Fail in OrderMapper - getNextOrderNo");
            System.out.println(e.getMessage());
        }
        */
        return (int)(Math.random()*999);
    }

}
